package com.neulogistics.contrib.procuredcenter.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blackto.lib.mediat.core.IMediator;
import com.neulogistics.contrib.procuredcenter.api.application.command.CreateGoodsCommand;
import com.neulogistics.contrib.procuredcenter.api.application.command.UpdateGoodsCommand;
import com.neulogistics.contrib.procuredcenter.api.application.query.GoodsQuery;
import com.neulogistics.contrib.procuredcenter.api.application.query.IGoodsQueryService;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.GoodsViewModel;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ErrorCode;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

@RestController
@Api(tags = "商品管理")
@RequestMapping("/procuredCenter/goods")
public class GoodsController {
    @Resource
    IMediator mediator;

    @Resource
    IGoodsQueryService goodsQueryService;

    @ApiOperation("查询商品可否退货")
    @GetMapping("/checkReturn/{goodsId}")
    public BaseResponse<Boolean> checkReturn(@PathVariable @NotNull @Positive Integer goodsId){
        GoodsViewModel goodsViewModel = goodsQueryService.getById(goodsId);
        Boolean optReturn = goodsViewModel.optReturn == 1;

        return goodsViewModel == null ?
                ResultUtils.error(ErrorCode.SYSTEM_ERROR)
                : ResultUtils.success(optReturn);
    }

    @ApiOperation("查询商品可否换货")
    @GetMapping("/checkExchange/{goodsId}")
    public BaseResponse<Boolean> checkExchange(@PathVariable @NotNull @Positive Integer goodsId){
        GoodsViewModel goodsViewModel = goodsQueryService.getById(goodsId);
        Boolean optReplace = goodsViewModel.optReplace == 1;

        return goodsViewModel == null ?
                ResultUtils.error(ErrorCode.SYSTEM_ERROR)
                : ResultUtils.success(optReplace);
    }


    @ApiOperation("新增商品")
    @PostMapping("/create")
    public BaseResponse<String> create(@RequestBody @Validated @NotNull CreateGoodsCommand command){

        BaseResponse<String> baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("修改商品")
    @PostMapping("/update")
    public BaseResponse update(@RequestBody @Validated @NotNull UpdateGoodsCommand command){

        BaseResponse baseResponse = mediator.send(command);

        return baseResponse == null ? ResultUtils.error(ErrorCode.SYSTEM_ERROR) : baseResponse;
    }

    @ApiOperation("根据id获取商品")
    @GetMapping("/get/{goodsId}")
    public BaseResponse<GoodsViewModel> get(@PathVariable @NotNull @Positive Integer goodsId){

        GoodsViewModel goodsViewModel = goodsQueryService.getById(goodsId);

        return ResultUtils.success(goodsViewModel);
    }

    @ApiOperation("分页查询商品")
    @PostMapping("/page")
    public BaseResponse<Page<GoodsViewModel>> page(@RequestBody @Validated @NotNull GoodsQuery goodsQuery){


        Page<GoodsViewModel> viewModelPage = goodsQueryService.page(goodsQuery);

        return ResultUtils.success(viewModelPage);
    }
}
